package com.youlai.system.controller;

import com.youlai.system.common.model.Option;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.form.DepartmentForm;
import com.youlai.system.model.form.DeptForm;
import com.youlai.system.model.form.ManagerForm;
import com.youlai.system.model.form.RoleForm;
import com.youlai.system.model.query.DeptQuery;
import com.youlai.system.model.vo.DepartmentVO;
import com.youlai.system.model.vo.DeptVO;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.service.DepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "08.我的部门接口")
@RestController
@RequestMapping("/api/v1/mydept")
@RequiredArgsConstructor
public class DepartController {

    private final DepartmentService departmentService;

    @Operation(summary = "获取部门列表")
    @GetMapping("/department")
    public Result<List<DepartmentVO>> listDepartments() {
        List<DepartmentVO> list = departmentService.listDepartments();
        return Result.success(list);
    }

    @Operation(summary = "新增或修改部门")
    @PostMapping
    @PreventDuplicateSubmit
    public Result saveDept(
            @Valid @RequestBody DepartmentForm formData
    ) {
        Integer id = departmentService.saveDept(formData);
        return Result.success(id);
    }


    @Operation(summary = "删除部门")
    @DeleteMapping("/{ids}")
    public Result deleteDepartments(
            @Parameter(description ="部门ID，多个以英文逗号(,)分割") @PathVariable("ids") String ids
    ) {
        boolean result = departmentService.deleteByIds(ids);
        return Result.judge(result);
    }

    @Operation(summary = "获取部门负责人数据")
    @GetMapping("/options")
    public Result<List<Option>> listManageOptions() {
        List<Option> list = departmentService.listManage();
        return Result.success(list);
    }


    @Operation(summary = "部门表单数据")
    @GetMapping("/{id}/form")
    public Result<DepartmentForm> getRoleForm(
            @Parameter(description = "部门id") @PathVariable Long id
    ) {
        DepartmentForm departmentForm = departmentService.getDepartmentForm(id);
        return Result.success(departmentForm);
    }
}
